﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TcomInvoice
{
    public partial class ListInvoice : Form
    {
        private DataHelper db = new DataHelper();
        public int? SelectInvoiceID { get; set; }

        public ListInvoice()
        {
            InitializeComponent();
        }

        private void ListInvoice_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = db.GetListInvoices();
            dataGridView2.DataSource = db.GetListCustomer();
            dataGridView3.DataSource = db.GetDictionary();
            dataGridView4.DataSource = db.GetInvoiceLine().ToList();
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            SelectInvoiceID = (int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
            this.Hide();
        }

        private void ListInvoice_FormClosing(object sender, FormClosingEventArgs e)
        {
            db.ApplyChanges();
        }

        private void dataGridView4_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            List<InvoiceLine> listLines = (dataGridView4.DataSource as List<InvoiceLine>);

            MessageBox.Show(listLines[e.RowIndex].Invoice.Date.ToString());
        }

        private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            int invoiceId = (int)e.Row.Cells["ID"].Value;
            db.DeleteInvoiceLine(invoiceId);
        }
    }
}
